//
//  Cell.m
//  自动计算单元格高度
//
//  Created by vera on 14-9-13.
//  Copyright (c) 2014年 vera. All rights reserved.
//

#import "Cell5_2.h"
#import "GBRequest.h"

#define docid @"&docId="
#define collectUrl @"http://192.168.1.220/favorite.do?method=add&token="
#define cancleCollectUrl @"http://192.168.1.220/favorite.do?method=cancelFav&token="

@implementation Cell5_2

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = WQColor(241, 243, 244);
        
        UIView *container_ = [[UIView alloc] initWithFrame:CGRectMake(5.5,4,screenW - 11,61)];
        container_.layer.cornerRadius = 3.0;
        container_.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:container_];
        
        // Initialization code
        _titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0,screenW - 90, 60)];
        _titleLabel1.numberOfLines = 1;
        [container_ addSubview:_titleLabel1];
        
        _image = [[UIImageView alloc] initWithFrame:CGRectMake(-10000, 15, 60, 54)];
        [container_ addSubview:_image];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 33, 50, 30)];
        _timeLabel.numberOfLines = 0;
        _timeLabel.textColor = [UIColor lightGrayColor];
        [container_ addSubview:_timeLabel];
        
        _lasttiemLabel = [[UILabel alloc] initWithFrame:CGRectMake(152, 33, 80, 30)];
        _lasttiemLabel.numberOfLines = 0;
        _lasttiemLabel.textColor = [UIColor lightGrayColor];
        [container_ addSubview:_lasttiemLabel];
        
        _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenW - 30, 5, 80, 30)];
        _typeLabel.numberOfLines = 0;
        _typeLabel.textColor = [UIColor lightGrayColor];
        [container_ addSubview:_typeLabel];
        
        _comeFromLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 33, 80, 30)];
        _comeFromLabel.numberOfLines = 0;
        _comeFromLabel.textColor = [UIColor lightGrayColor];
        [container_ addSubview:_comeFromLabel];
        
        _readBtn = [UIButton buttonWithType:0];
        _readBtn.frame = CGRectMake(-1000, 37, 25, 25);
        _readBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [_readBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_readBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_readBtn addTarget:self action:@selector(shanchu:) forControlEvents:UIControlEventTouchUpInside];
        [container_ addSubview:_readBtn];
        
        _deleteImage = [[UIImageView alloc] initWithFrame:CGRectMake(-1000, 39, 15, 15)];
        //NSLog(@"%f",self.contentView.frame.size.width);
        _deleteImage.image = [UIImage imageNamed:@"删除"];
        //[container_ addSubview:_deleteImage];
        //self.contentView.layer.cornerRadius = 10;
        //self.contentView.frame = CGRectMake(5, 3, screenW - 10, 59);
    }
    return self;
}

#pragma mark - 删除按钮的操作
-(void)shanchu:(UIButton *)sender
{
    KDAccount *account = [KDAccountTool account];

    
    //NSLog(@"%ld",(long)sender.tag);
    NSString *sssss = [NSString stringWithFormat:@"%ld%@",(long)sender.tag,@"Dele"];
    //NSUserDefaults
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //读取数据
    NSString *str = [userDefaults stringForKey:sssss];
    
    //取消收藏
    _deleteUrl = [NSString stringWithFormat:@"%@%@%@%@",cancleCollectUrl,account.tokens,docid,str];
    //NSLog(@"%@",_deleteUrl);
    NSString *kkkkk = [NSString stringWithFormat:@"%ld",(long)sender.tag];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kkkkk,@"deletetS", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleShow" object:nil userInfo:userInfo];
    
    [self requestData];
}

#pragma mark - 请求数据
-(void)requestData
{
    GBRequest *request1 = [[GBRequest alloc]initWithUrlStr:_deleteUrl];
    request1.tag = 1;
    request1.delegate = self;
    [request1 startRequestData];
}

#pragma mark - 数据成功请求完毕
- (void)requestDidFinished:(GBRequest *)request
{
    NSString *str = [[NSString alloc] initWithData:request.requestData encoding:NSUTF8StringEncoding];
    //解析
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    id dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *jsonDict = nil;
    //NSLog(@"%@",jsonDict);
    if ([dataArray isKindOfClass:[NSDictionary class]])
    {
        jsonDict = dataArray;
        //NSLog(@"%@",dict[@"status"]);
    }
    if([jsonDict[@"status"] isEqualToString:@"ok"])
    {
        
    }
}
- (void)requestDidFailedWithError:(NSError *)error{
    KDLog(@"%@", error);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
